<template>
  <div class="data-form notice-form" v-loading.fullscreen.lock="loading">
    <el-form label-width="auto" size="medium">
      <el-form-item label="公告标题">
        <el-input v-if="canUpdate" v-model="noticeForm.nitoceTitle" class="form-input"></el-input>
        <span v-else>{{noticeForm.nitoceTitle}}</span>
      </el-form-item>
      <el-form-item label="公告图片">
        <imgs-upload v-model="noticeForm.noticeImgUrl" :limit="1" :img-width="150" :img-height="150" :readonly="!canUpdate">
        </imgs-upload>
      </el-form-item>
      <el-form-item label="公告内容">
        <sys-area-input v-model="noticeForm.noticeContent" :autosize="{minRows:2,maxRows:5}" :disabled="!canUpdate" :maxlength="512"
          placeholder="公告内容不能超过512个字">
        </sys-area-input>
      </el-form-item>
      <el-form-item label="活动详情地址">
        <el-input v-if="canUpdate" v-model="noticeForm.noticeTipUrl" class="form-input"></el-input>
        <span v-else>{{noticeForm.noticeTipUrl}}</span>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="noticeForm.status" class="form-input">
          <el-option v-for="item in statusList" :key="item.value" :value="item.value" :label="item.label">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div style="text-align:center">
      <el-button size="medium" type="primary" @click="saveNotice">
        <span v-if="type==='add'">新增</span>
        <span v-else>修改</span>
      </el-button>
    </div>
  </div>
</template>

<script>
  import LogApi from 'services/logApi';

  import NoticeForm from 'models/log/noticeForm';

  import ImgsUpload from 'commonComponents/imgsUpload';
  import SysAreaInput from 'commonComponents/sysAreaInput';

  export default {
    props: {
      notice: {
        type: Object,
        default () {
          return null;
        }
      },
      type: {
        type: String,
        default: 'add'
      }
    },

    computed: {
      canUpdate() {
        let noticeForm = this.noticeForm;
        return this.type === 'add' || noticeForm.oldStatus === -1;
      }
    },

    data() {
      let noticeForm;
      if (this.type === 'add') {
        noticeForm = new NoticeForm();
      } else {
        noticeForm = _.cloneDeep(this.notice);
      }

      let statusList = [];
      if (this.type === 'add' || noticeForm.status === -1) {
        statusList = this.i18n.getDropDown('log.noticeMgr.status', [-1, 0, 1]);
      } else {
        statusList = this.i18n.getDropDown('log.noticeMgr.status', [0, 1]);
      }
      return {
        loading: false,
        noticeForm,
        statusList
      };
    },

    methods: {
      saveNotice() {
        let noticeForm = this.noticeForm;
        let code = noticeForm.checkValid();
        if (code !== '') {
          let msg = this.i18n.getMsg(`log.noticeMgr.error.${code}`);
          this.$message(msg);
          return;
        }

        if (this.type === 'add') {
          this.addNotice(noticeForm);
        } else {
          this.updateNotice(noticeForm);
        }
      },

      addNotice(noticeForm) {
        this.loading = true;
        LogApi.addNotice(noticeForm).then(() => {
          this.loading = false;
          this.$alert('公告新增成功', '信息', {
            callback: () => {
              this.$emit('save-success');
            }
          });
        }).catch(() => {
          this.loading = false;
        });
      },

      updateNotice(noticeForm) {
        this.loading = true;
        LogApi.updateNotice(noticeForm).then(() => {
          this.loading = false;
          this.$alert('公告修改成功', '信息', {
            callback: () => {
              this.$emit('save-success');
            }
          });
        }).catch(() => {
          this.loading = false;
        });
      }
    },

    components: {
      ImgsUpload,
      SysAreaInput
    }
  };

</script>

<style lang="scss" scoped>
  @import '~viewStyle/dataForm';

</style>
